package com.xdf.whiteaccount.utils;

import com.xdf.whiteaccount.enums.OpeEnum;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author chanchaw
 * @create 2023-04-08 15:41
 */
public class BeanUtils {
    public static boolean hasField(Class clazz, String fieldName) {
        Field field = ReflectionUtils.findField(clazz, fieldName);
        if (field != null) {
            return true;
        }
        return false;
    }

    public static boolean hasMethod(Class clazz, String methodName, Class[] argsType) {
        Method method = ReflectionUtils.findMethod(clazz, methodName, argsType);
        if (null != method) {
            return true;
        }
        return false;
    }

//    private static SysUser fullFillRecord(SysUser record) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        if(!hasField(SysUser.class,SystemConfig.ATTRIBUTE_NAME_CREATE_USER)
//                || !hasMethod(SysUser.class, SystemConfig.METHOD_NAME_CREATE_USER,new Class[]{String.class})) return record;
//        Class<? extends SysUser> clazz = record.getClass();
//        Method method1 = clazz.getDeclaredMethod(SystemConfig.METHOD_NAME_CREATE_USER, String.class);
//        method1.invoke(record,HttpRequestUtils.getRequestUserSid());
//        return record;
//    }

    /**
     * 为对象填充属性 createUser - 通过方法 setCreateUser
     * 在判断属性和方法都存在的情况下才会为对象属性赋值
     * 如果不存在则返回原始对象
     * 填写 request 中用户ID到属性 createUser 中
     * @param record 被装饰的对象
     * @return 装饰后的对象
     * @param <T> 泛型
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> T fullFillRecord(T record, OpeEnum opeEnum) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        return setCreateUser(record, opeEnum);
    }

    public static <T> T setCreateUser(T record, OpeEnum opeEnum) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        String attrName = "";
//        String methodName = "";
//        if(opeEnum == OpeEnum.NEW) {
//            attrName = SystemConfig.ATTRIBUTE_NAME_CREATE_USER;
//            methodName = SystemConfig.METHOD_NAME_CREATE_USER;
//        }
//        if(opeEnum == OpeEnum.UPDATE) {
//            attrName = SystemConfig.ATTRIBUTE_NAME_LAST_USER;
//            methodName = SystemConfig.METHOD_NAME_LAST_USER;
//        }
//
//        if(!hasField(record.getClass(), attrName)
//                || !hasMethod(record.getClass(), methodName, new Class[]{String.class})) return record;
//        Class<?> clazz = record.getClass();
//        Method method1 = clazz.getDeclaredMethod(methodName, String.class);
//        method1.invoke(record, HttpRequestUtils.getRequestUserSid());
        return record;
    }


    /**
     * 将map装换为javabean对象
     *
     * @param map
     * @param bean
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map, T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }

}
